﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NextAction.Domain.Abstract;
using NextAction.WebUI.Models;
using NextAction.Domain.Entities;
using System.Data.Objects.SqlClient;

namespace NextAction.WebUI.Controllers
{
    public class GtdProjectController : AbstractGtdController
    {
        public GtdProjectController(IGtdEnityRepository gtdProductRepository)
            : base(gtdProductRepository)
        {
        }
 
        public ViewResult Index()
        {
             return View(repository.GetGtdProjects());
        }

        public ViewResult Edit(int projectID)
        {
            return View(repository.GetProjectByID(projectID));
        }

        [HttpPost]
        public ActionResult Edit(GtdProject gtdProject)
        {
            if (ModelState.IsValid)
            {
                if (gtdProject.ProjectID != 0)
                {
                    gtdProject = repository.GetProjectByID(gtdProject.ProjectID);
                    UpdateModel(gtdProject);
                }

                repository.SaveGtdProject(gtdProject);
                return RedirectToAction("Index", "Desk");
            }
            else
            {
                return View(gtdProject);
            }
        }

        public ViewResult Create()
        {
            return View("Edit", new GtdProject());
        }

        private GtdTaskEditViewModel CreateGtdEditTaskViewModel(int taskID)
        {
            return new GtdTaskEditViewModel
            {
                GtdTask = repository.GtdTasks.FirstOrDefault(p => p.TaskID == taskID),
                GtdProjects = repository.GtdProjects.ToList()
            };
        }

        private GtdTaskEditViewModel CreateGtdEditTaskViewModel(GtdTask task)
        {
            return new GtdTaskEditViewModel
            {
                GtdTask = task,
                GtdProjects = repository.GtdProjects.ToList()
            };
        }

    }
}
